package com.acme.bankapp.domain.bank;

import com.acme.bankapp.ClientRegistrationListener;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Student on 18.08.2014.
 */
public class Bank {
    ClientRegistrationListener [] listeners;


    private Collection<Client> clients;

    public Bank(){
      this(new ClientRegistrationListener[]{new EmailNotificationListener(), new PrintClientListener()});
    }

    public Bank(ClientRegistrationListener[] listeners) {
        clients = new ArrayList<Client>();
        this.listeners = listeners;
    }

    public Bank (Collection<Client> clients){
        this.clients = clients;
    }

    public Collection<Client> getClients() {
        return clients;
    }

    public void addClient(Client client) throws ClientExistsException {
        //PrintClientListener(client);

        for (Client tempClient: clients){
            if (tempClient.getName().equals(client.getName())){
                throw new ClientExistsException();
            }
        }
        clients.add(client);
        for (ClientRegistrationListener listener: listeners){
            listener.onClientAdded(client);
        }
    }


    /**
     * Created by Student on 20.08.2014.
     */
    public static class EmailNotificationListener implements ClientRegistrationListener {
        @Override
        public void onClientAdded(Client client) {
            System.out.println("Notification email for client "+ client + " to be sent");
        }
    }

    /**
     * Created by Student on 20.08.2014.
     */
    public static class PrintClientListener implements ClientRegistrationListener{

        @Override
        public void onClientAdded(Client client) {
            System.out.println(client);
        }
    }
}
